<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org" lang="zh">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- 引入登录页面的样式文件 -->
    <link rel="stylesheet" href="/css/main.css">
    <link rel="stylesheet" href="/css/demo.css">
    <style>
        body {
            background-image: url("/images/background.jpg");
            background-repeat: repeat-x;
            background-size: cover;
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        /* 调整注册容器样式，使其与登录框样式一致 */
        .register-container {
            padding: 20px;
            border-radius: 5px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            /* 可以根据 main.css 和 demo.css 进行调整 */
        }

        h2 {
            text-align: center;
        }

        /* 输入框样式 */
        .user-box {
            position: relative;
        }

        .user-box input {
            width: 100%;
            padding: 10px 0;
            font-size: 16px;
            color: #fff;
            margin-bottom: 30px;
            border: none;
            border-bottom: 1px solid #fff;
            outline: none;
            background: transparent;
        }

        .user-box label {
            position: absolute;
            top: 0;
            left: 0;
            padding: 10px 0;
            font-size: 16px;
            color: #fff;
            pointer-events: none;
            transition: .5s;
        }

        .user-box input:focus ~ label,
        .user-box input:valid ~ label {
            top: -20px;
            left: 0;
            color: #03e9f4;
            font-size: 12px;
        }

        /* 按钮样式，模仿登录页面的链接样式 */
        button {
            position: relative;
            display: inline-block;
            padding: 10px 20px;
            color: #03e9f4;
            font-size: 16px;
            text-decoration: none;
            text-transform: uppercase;
            overflow: hidden;
            transition: .5s;
            margin-top: 40px;
            letter-spacing: 4px;
            background-color: transparent;
            border: none;
            cursor: pointer;
        }

        button span {
            position: absolute;
            display: block;
        }

        button span:nth-child(1) {
            top: 0;
            left: -100%;
            width: 100%;
            height: 2px;
            background: linear-gradient(90deg, transparent, #03e9f4);
            animation: btn-anim1 1s linear infinite;
        }

        @keyframes btn-anim1 {
            0% {
                left: -100%;
            }
            50%, 100% {
                left: 100%;
            }
        }

        button span:nth-child(2) {
            top: -100%;
            right: 0;
            width: 2px;
            height: 100%;
            background: linear-gradient(180deg, transparent, #03e9f4);
            animation: btn-anim2 1s linear infinite;
            animation-delay: .25s
        }

        @keyframes btn-anim2 {
            0% {
                top: -100%;
            }
            50%, 100% {
                top: 100%;
            }
        }

        button span:nth-child(3) {
            bottom: 0;
            right: -100%;
            width: 100%;
            height: 2px;
            background: linear-gradient(270deg, transparent, #03e9f4);
            animation: btn-anim3 1s linear infinite;
            animation-delay: .5s
        }

        @keyframes btn-anim3 {
            0% {
                right: -100%;
            }
            50%, 100% {
                right: 100%;
            }
        }

        button span:nth-child(4) {
            bottom: -100%;
            left: 0;
            width: 2px;
            height: 100%;
            background: linear-gradient(360deg, transparent, #03e9f4);
            animation: btn-anim4 1s linear infinite;
            animation-delay: .75s
        }

        @keyframes btn-anim4 {
            0% {
                bottom: -100%;
            }
            50%, 100% {
                bottom: 100%;
            }
        }

        button:hover {
            background: #03e9f4;
            color: #fff;
            border-radius: 5px;
            box-shadow: 0 0 5px #03e9f4,
            0 0 25px #03e9f4,
            0 0 50px #03e9f4,
            0 0 100px #03e9f4;
        }
    </style>
</head>
<body class="dark">
<div class="register-container">
    <h2>注册</h2>
    <form id="registerForm">
        <div class="user-box">
            <input type="text" id="username" required>
            <label>用户名</label>
        </div>
        <div class="user-box">
            <input type="text" id="nickname" required>
            <label>昵称</label>
        </div>
        <div class="user-box">
            <input type="password" id="password" required>
            <label>密码</label>
        </div>
        <div class="user-box">
            <input type="password" id="repassword" required>
            <label>确认密码</label>
        </div>
        <button type="button" onclick="register()">
            <span></span>
            <span></span>
            <span></span>
            <span></span>
            注册
        </button>
    </form>
    <p id="errorMessage" style="color: red;"></p>
</div>
<script>
    function register() {
        const username = document.getElementById('username').value;
        const nickname = document.getElementById('nickname').value;
        const password = document.getElementById('password').value;
        const repassword = document.getElementById('repassword').value;

        fetch('/register', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded'
            },
            body: `username=${username}&nickname=${nickname}&password=${password}&repassword=${repassword}`
        })
            .then(response => response.json())
            .then(data => {
                if (data.success) {
                    // 存储用户名和密码到 localStorage
                    localStorage.setItem('registeredUsername', username);
                    localStorage.setItem('registeredPassword', password);
                    window.location.href = '/login';
                } else {
                    document.getElementById('errorMessage').innerHTML = data.message;
                }
            })
            .catch(error => {
                console.error('请求出错:', error);
                document.getElementById('errorMessage').innerHTML = '网络错误，请稍后重试';
            });
    }
</script>
</body>
</html>